package com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle;

import com.lkm.frame.P;
import com.lkm.frame.otto.LinkBus;
import com.lkm.javatool.NumberHelp;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsReceiverDataHandle;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDEntity;
import com.lkm.market2_0.m_imple.steelyardserver.server.ICMDHandle;
import com.lkm.market2_0.m.steelyardserver.event.OnSubtotalEvent;



public class CMDWeightADHandle implements ICMDHandle{

    SteelyardDataModule mSteelyardDataModule;
    SteelyardServerModule mSteelyardServerModule;

    public CMDWeightADHandle() {
        this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        this.mSteelyardServerModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
    }

    
    public static int getAD(int num,byte [] data){
        if(data.length<30){
            return 0;
        }
        int offset = 2 + 4;
        return NumberHelp.bytesToIntDesc(new byte[]{data[14-offset], data[15-offset], data[16-offset], data[17-offset]});
    }
    
    public static int getZeroAD(int num,byte [] data){
        if(data.length<30){
            return 0;
        }
        int offset = 2 + 4;
        return NumberHelp.bytesToIntDesc(new byte[]{data[18-offset], data[19-offset], data[20-offset], data[21-offset]});
    }
    
    public static int getOffsetZeroAD(int num,byte [] data){
        if(data.length<30){
            return 0;
        }
        int offset = 2 + 4;
        return NumberHelp.bytesToIntDesc(new byte[]{data[22-offset], data[23-offset], data[24-offset], data[25-offset]});
    }
    
    public static int getKvalue(int num,byte [] data){
        if(data.length<30){
            return 0;
        }
        int offset = 2 + 4;
        return NumberHelp.bytesToIntDesc(new byte[]{data[26-offset], data[27-offset], data[28-offset], data[29-offset]});
    }
    @Override
    public AbsReceiverDataHandle handle(CMDEntity cmdEntity) {
        if(cmdEntity.is32Zero()){
            return null;
        }
        if(this.mSteelyardDataModule==null) {
            this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        }
        if(this.mSteelyardServerModule==null) {
            this.mSteelyardServerModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
        }

        byte[] data= cmdEntity.getData();

        int ad = getAD(cmdEntity.getNum(),data);

        if(P.isDebug()&&false){
            P.d("====称重AD"+ad);
        }

        mSteelyardDataModule.AD(cmdEntity.getSteelyardMac(),ad,getZeroAD(cmdEntity.getNum(),data),getOffsetZeroAD(cmdEntity.getNum(),data),getKvalue(cmdEntity.getNum(),data));

        SteelyardInfo steelyardInfo = mSteelyardDataModule.getSteelyardInfo(cmdEntity.getSteelyardMac());
        if(steelyardInfo!=null){

            boolean isSalesJoin = steelyardInfo.hasState(SteelyardInfo.RunTimeState_salesJoin);
            if(steelyardInfo.hasState(SteelyardInfo.RunTimeState_isPrint)||
                    (isSalesJoin )){

                CMDPrintHandle.xx(mSteelyardDataModule,mSteelyardServerModule,cmdEntity.getSteelyardMac(),steelyardInfo,isSalesJoin);


            }


//            if(P.isDebug()){
//                P.d("====称重AD all"+ad);
//            }
            
            if(steelyardInfo.hasState(SteelyardInfo.RunTimeState_isAddXJ))
                
            if(steelyardInfo.hasState(SteelyardInfo.RunTimeState_subtotal)){

//                if(P.isDebug()){
//                    P.d("====称重AD 小计 "+ad);
//                }

                
                OrderEntity.GoodEntity goodEntity = mSteelyardDataModule.regLastWeightChangeToOrderGood(cmdEntity.getSteelyardMac());

                if(ExpandConfig.instance.isUI()){
//                    List<OrderEntity.GoodEntity> goodEntities = mSteelyardDataModule.getGoodEntities(cmdEntity.getSteelyardMac());

                    
                    if(goodEntity!=null){
                        OnSubtotalEvent onSubtotalEvent = new OnSubtotalEvent(cmdEntity.getSteelyardMac(),goodEntity);
                        
                        onSubtotalEvent.setSyncForDevice(true);
                        LinkBus.sendBroadcast(onSubtotalEvent);
                    }

                }
            }

        }

        mSteelyardServerModule.tickOK_block(cmdEntity.getSteelyardMac());
        return null;
    }
}
